﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Text.RegularExpressions;
namespace gamexpert
{
    public class Cart
    {
        List<Product> product;

        // Declare the delegate (if using non-generic pattern).
        public delegate void cartChangeEventHandler(object sender, cartChangeEventArgs e);
        // Declare the event.
        public event cartChangeEventHandler changeEvent;

        public List<Product> GetProductList()
        {
            return product;
        }
        public Cart()
        {
            product=new List<Product>();
        }
        public void AddProduct(Product p){
            bool found = false;
            for (int i = 0; i < product.Count; i++)
            {
                if (product[i].Id == p.Id)
                {
                    Product tmp_p = product[i];
                    product.RemoveAt(i);
                    tmp_p.Qty = tmp_p.Qty + p.Qty;
                    product.Add(tmp_p);
                    changeEvent(this, new cartChangeEventArgs("AddProduct"));
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                product.Add(p);
                changeEvent(this, new cartChangeEventArgs("AddProduct"));
            }
        }
        public void AddProductList(List<Product> ps)
        {
            product = null;
            product = ps;
            changeEvent(this, new cartChangeEventArgs("AddProductList"));
        }
        public void RemoveProduct(Product p)
        {
            for (int i = 0; i < product.Count; i++)
            {
                if (product[i].Id == p.Id)
                {
                    product.RemoveAt(i);
                    changeEvent(this, new cartChangeEventArgs("RemoveProduct"));
                    break;
                }
            }
        }
        public void RemoveProductById(int id)
        {
            for (int i = 0; i < product.Count; i++)
            {
                if (product[i].Id == id)
                {
                    product.RemoveAt(i);
                    changeEvent(this, new cartChangeEventArgs("RemoveProductById"));
                    break;
                }
            }
        }
        public void RemoveAllProducts()
        {
            product.Clear();
            changeEvent(this, new cartChangeEventArgs("RemoveProductById"));
        }
        public double Total()
        {
            double total=0.0;
            foreach (Product p in product)
            {
                total += p.Total();
            }
            return total;
        }
        public int TotalQty()
        {
            int total = 0;
            foreach (Product p in product)
            {
                total += p.Qty;
            }
            return total;
        }
        public double TotalShippingCost()
        {
            double total = 0.0;
            foreach (Product p in product)
            {
                total += p.TotalShippingCost();
            }
            return total;
        }
        public double GST()
        {
            return (7);
        }
        public double finalTotal()
        {
            return (this.Total());
        }
        public void fireChangeEvent()
        {
            changeEvent(this, new cartChangeEventArgs("Manual Fire"));
        }
        public Product FindProductById(int id)
        {
            int tmp=0;
            for (int i = 0; i < product.Count; i++)
            {
                if (product[i].Id == id)
                {
                    tmp=i;
                    break;
                }
            }
            return product[tmp];
        }

    }
    public class cartChangeEventArgs
    {
        public cartChangeEventArgs(string s) { Text = s; }
        public String Text { get; private set; } // readonly
    }
   
}